/*
    This file is part of Epoch <http://code.google.com/p/epoch/>.
    
    Copyright (C) 2011  Adam Meily <meily.adam@gmail.com>

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License along
    with this program; if not, write to the Free Software Foundation, Inc.,
    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/


#include "ShareFile.h"
#include <QtCore/QCryptographicHash>

ShareFile::ShareFile(const ShareDirectory& parent) : m_size(0), m_parent(parent)
{

}


ShareFile::ShareFile(const ShareDirectory& parent, const QString& name)
        : m_parent(parent), m_size(0), m_name(name)
{
    calcUuid();
}


ShareFile::ShareFile(const ShareFile& other)
        : m_name(other.m_name),m_lastModified(other.m_lastModified),
          m_size(other.m_size), m_parent(other.m_parent), m_uuid(other.m_uuid)
{
}

ShareFile::ShareFile() : m_size(0)
{

}


ShareFile::~ShareFile()
{

}

ShareFile& ShareFile::operator=(const ShareFile& other)
{
    m_name = other.m_name;
    m_uuid = other.m_uuid;
    m_lastModified = other.m_lastModified;
    m_parent = other.m_parent;
    m_size = other.m_size;
    return *this;
}

ShareFile ShareFile::fromRecord(const ShareDirectory& parent,
                                const Record& record, bool strict)
{
    bool ok;
    ShareFile sf(parent);
    if(!record["last-modified"].isEmpty())
    {
        qint64 ts = record["last-modified"].toLongLong(&ok, 10);
        if(ok)
            sf.m_lastModified = QDateTime::fromMSecsSinceEpoch(ts);
    }
    
    if(!record["size"].isEmpty())
    {
        quint64 size = record["size"].toULongLong(&ok, 10);
        if(ok)
            sf.m_size = size;
    }
    
    if(!record["uuid"].isEmpty())
    {
        QByteArray uuid = QByteArray::fromHex(record["size"].toUtf8());
        if(uuid.length() == 20)
            sf.m_uuid = uuid;
    }
    
    if(!record["name"].isEmpty())
        sf.m_name = record["name"];
    
    if(sf.m_uuid.isEmpty() && !sf.m_name.isEmpty())
        sf.calcUuid();
    
    if(strict && (sf.m_lastModified.isNull() || sf.m_name.isEmpty() || sf.m_uuid.isEmpty()))
        sf = ShareFile();
    
    return sf;
}

const QDateTime& ShareFile::lastModified() const
{
    return m_lastModified;
}

const ShareDirectory& ShareFile::parent() const
{
    return m_parent;
}

quint64 ShareFile::size() const
{
    return m_size;
}

void ShareFile::calcUuid()
{
    QString temp =  m_parent.getPath(m_name);
    m_uuid = QCryptographicHash::hash(m_parent.share().uuid() +
                                      m_parent.getPath(m_name).toUtf8(),
                                      QCryptographicHash::Sha1);
}

bool ShareFile::isNull() const
{
    return m_uuid.isEmpty();
}

const QString& ShareFile::name() const
{
    return m_name;
}

const QByteArray& ShareFile::uuid() const
{
    return m_uuid;
}

Record ShareFile::toRecord() const
{
    Record r;
    if(isNull())
        return r;
    
    r.insert("last-modified", QString::number(m_lastModified.toMSecsSinceEpoch(), 10));
    r.insert("name", m_name);
    //r.insert("uuid", m_uuid.toHex());
    r.insert("size", QString::number(m_size, 10));
    return r;
}


